package com.linkme2night.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.facebook.Session;
import com.linkme2night.R;
import com.linkme2night.view.login.ChooseLoginActivity;

public class CustomMenuActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_custom_menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {  
		switch (item.getItemId()) {

		case R.id.menu_sys_btnLogout:
			executeLogout();
			return true;

		}
	     
	   return true;
	}

	private void executeLogout() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		 
        // Setting Dialog Title
        alertDialog.setTitle("Logout...");
 
        // Setting Dialog Message
        alertDialog.setMessage("Are you sure you want to log out?");
 
        // Setting Icon to Dialog
        alertDialog.setIcon(R.drawable.logout);
 
        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
            	logout();
            }
        });
 
        // Setting Negative "CANCEL" Button
        alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	dialog.cancel();
            }
        });
 
        alertDialog.show();
	}
	
	
	private void logout(){
		if (Session.getActiveSession() != null){
			//logout
			Session.getActiveSession().closeAndClearTokenInformation();
			
			//Redirect to Login Menu
			Intent intent = new Intent(this, ChooseLoginActivity.class);
			this.startActivity(intent);
			this.finish();
		}
	}
}
